

//
//  HMNavigationController.m
//  黑马微博-wzy
//
//  Created by Mac on 2016/11/26.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "HMNavigationController.h"

@interface HMNavigationController ()

@end

@implementation HMNavigationController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
    }
    return  self;
}



//当第一次使用这个类的时候调用,且只调用一次
+ (void)initialize
{
    //只打印一次
//    HMLog(@"********** initialize *****");
    
    //设置 NavgiationBar 主题
    [self setupNavgiationBarTheme];
    
    
    //设置 UIBarButtonItem 主题
    [self setupUIBarButtonItemTheme];

}

//设置 NavgiationBar 主题
+ (void)setupNavgiationBarTheme{
    UINavigationBar *appearance = [UINavigationBar appearance];
    
    //设置导航栏背景
    [appearance setBackgroundImage:[UIImage imageNamed:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];

    //设置 文字 属性
    NSMutableDictionary *textArttrs = [NSMutableDictionary dictionary];
    textArttrs[NSFontAttributeName] = HMNavigationTitleFont;
    textArttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    //删除 文字 阴影
    textArttrs[NSShadowAttributeName] = nil;
    [appearance setTitleTextAttributes:textArttrs];
    
}


//设置 UIBarButtonItem 主题
+ (void)setupUIBarButtonItemTheme{

    //appearance 通过appearance 能设置整个项目中所有UIBarButtonItem
    UIBarButtonItem *appearance = [UIBarButtonItem appearance];
    //设置普通状态文字 属性
    NSMutableDictionary *textArttrs = [NSMutableDictionary dictionary];
    textArttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    textArttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    //    textArttrs[UITextAttributeFont]
    [appearance setTitleTextAttributes:textArttrs forState:UIControlStateNormal];
    
    //设置高亮状态文字 属性
    NSMutableDictionary *highTextArttrs = [NSMutableDictionary dictionary];
    highTextArttrs[NSForegroundColorAttributeName] = [UIColor redColor];
    [appearance setTitleTextAttributes:highTextArttrs forState:UIControlStateHighlighted];
    
    //设置不可点状态文字 属性
    NSMutableDictionary *disableTextArttrs = [NSMutableDictionary dictionary];
    disableTextArttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    [appearance setTitleTextAttributes:disableTextArttrs forState:UIControlStateDisabled];
    
    //设置背景
    //为了让某个按钮的背景消失,可以设置一张完全透明的图片
    [appearance setBackgroundImage:[UIImage imageWithName:@"navigationbar_button_background"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //该工程有4个HMNavigationController,会打印4次
//    HMLog(@"HMNavigationController === ");

}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//    NSLog(@"=== %@--- %ld", viewController, self.viewControllers.count);
    
    //栈,放所有子控制器
    if (self.viewControllers.count > 0 ) {
        //如果现在push 的不是栈底控制器
        viewController.hidesBottomBarWhenPushed = YES;
        //拦截 push 设置导航栏 按钮
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_back" hightImageName:@"navigationbar_back_highlighted" target:self action:@selector(back)];
        viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_more" hightImageName:@"navigationbar_more_highlighted" target:self action:@selector(more)];
    }
    
    [super pushViewController:viewController animated:animated];
    
    
}

- (void)back{
    //注意 是self, 因为本身就是 navgiationController
    [self popViewControllerAnimated:YES];
}

- (void)more{
    [self popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
